package com.yy.train.ticket.train.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.train.ticket.goout.Line;
import com.yy.train.ticket.into.TtLine;
import com.yy.train.ticket.train.dao.TtLineDao;
import com.yy.train.ticket.train.dao.TtLineStationDao;
import com.yy.train.ticket.train.entity.TtLineEntity;
import com.yy.train.ticket.train.entity.TtLineStationEntity;
import com.yy.train.ticket.train.service.TtLineService;
import com.yy.train.ticket.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service("ttLineService")
public class TtLineServiceImpl extends ServiceImpl<TtLineDao, TtLineEntity> implements TtLineService {


    @Resource
    TtLineDao ttLineDao;
    @Resource
    TtLineStationDao ttLineStationDao;

    @Override
    public R saveTtLine(TtLine ttLine) {
         String lineName=        ttLine.getLineName();
        List<Long> stationIds=ttLine.getStationIds();
        TtLineEntity ttLineEntity=new TtLineEntity();
        ttLineEntity.setLineName(lineName);
        ttLineEntity.setStartStationId(stationIds.get(0));
        ttLineEntity.setEndStationId(stationIds.get(stationIds.size()-1));
        this.ttLineDao.insert(ttLineEntity);
        Long lineId=ttLineEntity.getLineId();
        TtLineStationEntity ttLineStationEntity;
        for (int i = 0; i <stationIds.size() ; i++) {
            Long stationId=stationIds.get(i);
            ttLineStationEntity=new TtLineStationEntity();
            ttLineStationEntity.setLineId(lineId);
            ttLineStationEntity.setStationId(stationId);
            ttLineStationEntity.setSeq(i);
            this.ttLineStationDao.insert(ttLineStationEntity);
        }
        return R.ok();
    }

    @Override
    public R getLineById(Long lineId) {
        TtLineEntity ttLine=this.ttLineDao.selectById(lineId);
        List<TtLineStationEntity>  list= this.ttLineStationDao.selectList(Wrappers.<TtLineStationEntity>lambdaQuery().eq(TtLineStationEntity::getLineId,lineId));
        Line line=new Line();
        line.setLine(ttLine);
        line.setLineStations(list);
        return R.ok(line);
    }

    @Override
    public R updateTtLineById(TtLine ttLine) {
        String lineName=        ttLine.getLineName();
        List<Long> stationIds=ttLine.getStationIds();
        TtLineEntity ttLineEntity=new TtLineEntity();
        ttLineEntity.setLineId(ttLine.getLineId());
        ttLineEntity.setLineName(lineName);
        ttLineEntity.setStartStationId(stationIds.get(0));
        ttLineEntity.setEndStationId(stationIds.get(stationIds.size()-1));
        this.ttLineStationDao.delete(Wrappers.<TtLineStationEntity>lambdaQuery().eq(TtLineStationEntity::getLineId,ttLine.getLineId()));
        this.ttLineDao.updateById(ttLineEntity);
        Long lineId=ttLineEntity.getLineId();
        TtLineStationEntity ttLineStationEntity;
        for (int i = 0; i <stationIds.size() ; i++) {
            Long stationId=stationIds.get(i);
            ttLineStationEntity=new TtLineStationEntity();
            ttLineStationEntity.setLineId(lineId);
            ttLineStationEntity.setStationId(stationId);
            ttLineStationEntity.setSeq(i);
            this.ttLineStationDao.insert(ttLineStationEntity);
        }
        return R.ok();
    }
}